package com.app.service.impl;

import com.app.service.FormUrlService;
import com.app.mapper.FormUrlMapper;
import com.app.model.FormUrlModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class FormUrlServiceImpl implements FormUrlService {
   @Autowired
   FormUrlMapper formUrlMapper;

    @Override
    public FormUrlModel getUrlById(int id) {
        QueryWrapper<FormUrlModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        FormUrlModel formUrlModel =  formUrlMapper.selectOne(queryWrapper);
        if (formUrlModel == null) {
            return null;
        } else {
            return formUrlModel;
        }
    }

    @Override
    public List<FormUrlModel> getUrlByFormId(Integer formId, Integer type) {
        QueryWrapper<FormUrlModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if(formId != null){
            queryWrapper.eq("form_id", formId);
        }
        if(type != null){
            queryWrapper.eq("type", type);
        }
        List<FormUrlModel> plantModel =  formUrlMapper.selectList(queryWrapper);
        if (plantModel == null) {
            return null;
        } else {
            return plantModel;
        }
    }

    //    @Override
//    public Page<PlantModel> getPagePlantList(int page, int limit,String name,List<Integer> idList) {
//        QueryWrapper<PlantModel> queryWrapper = new QueryWrapper<>();
//        if(idList.size()>0){
//                queryWrapper.select().in("department_id",idList);
//        }else {
//            queryWrapper.select();
//        }
//        if(name != null){
//            queryWrapper.select().like("name",name);
//        }
//        queryWrapper.orderByDesc("id");
//        Page<PlantModel> pages = new Page<>();
//        pages.setCurrent(page);
//        pages.setSize(limit);
//        Page<PlantModel> plantList = plantMapper.selectPage(pages,queryWrapper);
//        return plantList;
//    }
    @Override
    public void update(FormUrlModel model) {
        formUrlMapper.updateById(model);
    }

    @Override
    public void insert(FormUrlModel model) {
        formUrlMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        formUrlMapper.deleteById(id);
    }

//    @Override
//    public Integer plantCount(List<Integer> idsList) {
//        QueryWrapper<PlantModel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("id");
//        if (idsList != null&&idsList.size()>0) {
//            queryWrapper.in("department_id", idsList);
//        }
//        Integer plantCount = plantMapper.selectCount(queryWrapper);
//        return plantCount;
//    }

//    @Override
//    public List<PlantModel> getStatisticsByIds(List<Integer> idList, String key) {
//        QueryWrapper<PlantModel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select(key,"COUNT(*) as count");
//        if (idList.size() != 0) {
//            queryWrapper.in("department_id", idList);
//        }
//        queryWrapper.groupBy(key);
//        List<PlantModel> personModelList = plantMapper.selectList(queryWrapper);
//        return personModelList;
//    }
//
//    @Override
//    public List<PlantModel> getPlantByIds(List<Integer> idList) {
//        QueryWrapper<PlantModel> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select().in("id",idList);
//        List<PlantModel> personModelList = plantMapper.selectList(queryWrapper);
//        return personModelList;
//    }
}
